package com.yixin.hospital.controller;

import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.common.Constant;
import com.yixin.hospital.entity.dto.req.template.TemplateAddReqDTO;
import com.yixin.hospital.entity.vo.ListTemplateVO;
import com.yixin.hospital.service.TemplateService;
import com.yixin.hospital.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/template")
@Api(value = "模板管理", tags = "模板管理")
@Slf4j
public class TemplateController {

    @Resource
    private TemplateService templateService;

    @GetMapping("/list")
    @ApiOperation(value = "查询科室模板")
    public ApiResult<List<ListTemplateVO>> list(@RequestParam(value = "jobOfficeId", required = false) Long jobOfficeId) {
        return templateService.allList(jobOfficeId);
    }

    @PostMapping("/addOrUpdate")
    @ApiOperation(value = "添加/编辑模板")
    public ApiResult<?> add(@Valid @RequestBody TemplateAddReqDTO templateAddReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return templateService.add(templateAddReqDTO, userName);
    }

    @DeleteMapping("/remove/{id}")
    @ApiOperation(value = "删除模板")
    public ApiResult remove(@PathVariable int id) {
        templateService.removeById(id);
        return ApiResult.success();
    }
}
